package cn.com.infohold.smartcity.api;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.support.annotation.NonNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by apple on 2017/4/24.
 */

public class UploadUtils {
    public static final int BITMAP_MAX_SIZE = 800;

    public static File getResizedBitmapFile(File file, @NonNull File outputFile) throws IOException {
        Object is = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        is = new FileInputStream(file);
        BitmapFactory.decodeStream((InputStream) is, (Rect) null, options);
        if (options.outWidth != -1 && options.outHeight != -1) {
            int sampleSize;
            for (sampleSize = 1; options.outHeight / sampleSize > BITMAP_MAX_SIZE && options.outWidth / sampleSize > BITMAP_MAX_SIZE; ++sampleSize) {
                ;
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = sampleSize;
            if (((InputStream) is).markSupported()) {
                ((InputStream) is).reset();
            } else {
                ((InputStream) is).close();
                is = new FileInputStream(file);
            }

            int sourceDegree = getBitmapDegree(file.getPath());
            Bitmap bitmap;
            if (sourceDegree == 0) {
                bitmap = BitmapFactory.decodeStream((InputStream) is, (Rect) null, options);
            } else {
                bitmap = rotateBitmapByDegree(BitmapFactory.decodeStream((InputStream) is, (Rect) null, options), sourceDegree);
            }

            FileOutputStream stream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 85, stream);
            stream.flush();
            stream.close();
            bitmap.recycle();
            if (is != null) {
                ((InputStream) is).close();
            }

            return outputFile;
        } else {
            return null;
        }
    }

    public static int getBitmapDegree(String path) {
        short degree = 0;

        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return degree;
    }

    public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
        if (degree % 360 == 0)
            return bm;
        Bitmap returnBm = null;
        Matrix matrix = new Matrix();
        matrix.postRotate((float) degree);

        try {
            returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        } catch (OutOfMemoryError var5) {
            ;
        }

        if (returnBm == null) {
            returnBm = bm;
        }

        if (bm != returnBm) {
            bm.recycle();
        }

        return returnBm;
    }

}
